{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6502f6fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ee06904",
   "metadata": {},
   "source": [
    "# 回顾"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "512f0cbf",
   "metadata": {},
   "source": [
    "## ConversationSummaryBufferMemory\n",
    "https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/memory/summary_buffer.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "73e23e2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import AzureChatOpenAI\n",
    "from langchain.memory import ConversationSummaryBufferMemory\n",
    "from langchain.chains import ConversationChain\n",
    "\n",
    "llm = AzureChatOpenAI(deployment_name=\"gpt4\", temperature=0.3, max_tokens=1000,\n",
    "                     streaming=True)\n",
    "\n",
    "#memory = ConversationSummaryBufferMemory(k=10) \n",
    "memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000)\n",
    "\n",
    "def get_response(input):\n",
    "    print(\"------------\")\n",
    "    print(memory.load_memory_variables({}))\n",
    "    print(\"------------\")\n",
    "    conversation_with_memory = ConversationChain(\n",
    "        llm=llm, \n",
    "        memory=memory,\n",
    "        verbose=False\n",
    "    )\n",
    "    return conversation_with_memory.predict(input=input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c2142230",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7868\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------\n",
      "{'history': ''}\n",
      "------------\n",
      "------------\n",
      "{'history': 'Human: 我叫蔡超，你的名字呢？\\nAI: 你好，蔡超。我是一个人工智能，所以我没有一个具体的名字。你可以称呼我为AI助手。'}\n",
      "------------\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "def respond(message, chat_history):\n",
    "        bot_message = get_response(message)\n",
    "        chat_history.append((message, bot_message))\n",
    "        return \"\", chat_history\n",
    "\n",
    "with gr.Blocks() as demo:\n",
    "    chatbot = gr.Chatbot(height=240) #对话框\n",
    "    msg = gr.Textbox(label=\"Prompt\") #输入框\n",
    "    btn = gr.Button(\"Submit\") #提交按钮\n",
    "    #提交\n",
    "    btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])\n",
    "    msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) \n",
    "gr.close_all()\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7afabf1d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
